package com.yxw.system.controller;

import com.atguigu.model.system.SysUser;
import com.atguigu.model.vo.LoginVo;
import com.yxw.common.result.Result;
import com.yxw.common.utils.JwtHelper;
import com.yxw.common.utils.MD5;
import com.yxw.system.exception.YXWException;
import com.yxw.system.service.SysUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: com.yxw.system.controller.IndexController
 * @description: TODO
 * @author: tommy
 * @create: 2023-07-23 下午 02:42
 */
@Api(tags = "用户登录接口")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 登录
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        //根据username查询数据
        SysUser sysUser =  sysUserService.getUserInfoByUserName(loginVo.getUsername());
        //如果查询为空
        if (sysUser == null) {
            throw new YXWException(20001,"用户不存在");
        }
        //判断密码是否一致
        String password = loginVo.getPassword();
        String encrypt = MD5.encrypt(password);
        if (!sysUser.getPassword().equals(encrypt)) {
            throw new YXWException(20002,"密码错误");
        }
        //判断用户状态是否可用
        if (sysUser.getStatus().intValue() == 0) {
            throw new YXWException(30001,"该用户被禁用请联系管理员!(。・＿・。)ﾉI’m sorry~");
        }
        //根据userid和username生成token字符串，通过map返回
        String token = JwtHelper.createToken(sysUser.getId(), sysUser.getUsername());

        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        return Result.ok(map);

    }


//        {
//            "code": 20000,
//                "data": {
//            "roles": [
//            "admin"
//        ],
//            "introduction": "I am a super administrator",
//                    "avatar": "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
//                    "name": "Super Admin"
//        }
//        }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("info")
    public Result getInfo(HttpServletRequest req) {
        //获取请求头token字符串
        String token = req.getHeader("token");
        //从token字符串获取用户名称（id)
        String username = JwtHelper.getUsername(token);
        //根据用户名称获取用户信息（基本信息 和 菜单权限 按钮权限数据）
        Map<String, Object> map = sysUserService.getUserInfo(username);

//        new HashMap<>();
//        map.put("roles","[admin]");
//        map.put("introduction","I am a super administrator");
//        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
//        map.put("name","Super Admin");
        return Result.ok(map);
    }

    /**
     * 退出登录
     * @return
     */
   @PostMapping("logout")
    public Result logout() {
        return Result.ok();
   }

}
